package com.eben.GUI;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.TextView;

import com.eben.R;
import com.eben.model.Highscore;
import com.eben.preferences.Preferences;

/**
 * This class represent the highscore list that is viewed to the user.
 * 
 * @author Jacob
 *
 */
public class HighScoreActivity extends Activity{
	private TextView namefirst,namesecond,namethird,namefourth,namefifth,namesixth,nameseventh,nameeighth,nameninth,nametenth;
	private TextView scorefirst,scoresecond,scorethird,scorefourth,scorefifth,scoresixth,scoreseventh,scoreeighth,scoreninth,scoretenth;
	private ArrayList<TextView> nameViewArray, scoreViewArray;
	private Button clean;
	private Context context;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.highscorelayout);
        context = this;
        
		final Builder dialog = new AlertDialog.Builder(this);
	    dialog.setTitle("Clear Highscores?");
	    dialog.setMessage("Are you sure you want to clear the highscorelist?");
	    dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
	        public void onClick(DialogInterface dialog, int whichButton) {
	        	Preferences.cleanHighscores(context);
	        	updateHighscoreLayout(context);
	        }
	    });
	    dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
	        public void onClick(DialogInterface dialog, int whichButton) {
	        	dialog.dismiss();
	        }
	    });
        
        
        
        //The clean button
        clean = (Button)this.findViewById(R.id.clean);
        clean.setOnTouchListener(new OnTouchListener() {
			
			public boolean onTouch(View v, MotionEvent e) {
				if(v.getId() == clean.getId()){
					clean.setBackgroundResource(R.drawable.clear_highscore_ontouch);
					if(e.getAction() == 1){
	                	clean.setBackgroundResource(R.drawable.clear_highscore);
	    				dialog.show();
	    				v.invalidate();
	                }
				}
				return false;
			}
		});
        
        // get all the paths from the xml layout
        namefirst = (TextView)this.findViewById(R.id.namefirst);namesecond = (TextView)this.findViewById(R.id.namesecond);namethird = (TextView)this.findViewById(R.id.namethird);
		namefourth = (TextView)this.findViewById(R.id.namefourth);namefifth = (TextView)this.findViewById(R.id.namefifth);namesixth = (TextView)this.findViewById(R.id.namesixth);
		nameseventh = (TextView)this.findViewById(R.id.nameseventh);nameeighth = (TextView)this.findViewById(R.id.nameeighth);nameninth = (TextView)this.findViewById(R.id.nameninth);
		nametenth = (TextView)this.findViewById(R.id.nametenth);
		scorefirst = (TextView)this.findViewById(R.id.scorefirst);scoresecond = (TextView)this.findViewById(R.id.scoresecond);scorethird = (TextView)this.findViewById(R.id.scorethird);
		scorefourth = (TextView)this.findViewById(R.id.scorefourth);scorefifth = (TextView)this.findViewById(R.id.scorefifth);scoresixth = (TextView)this.findViewById(R.id.scoresixth);
		scoreseventh = (TextView)this.findViewById(R.id.scoreseventh);scoreeighth = (TextView)this.findViewById(R.id.scoreeighth);scoreninth = (TextView)this.findViewById(R.id.scoreninth);
		scoretenth = (TextView)this.findViewById(R.id.scoretenth);
		
		nameViewArray = new ArrayList<TextView>(); // contains the textviews for the name
		scoreViewArray = new ArrayList<TextView>(); // contains the texviews for the score connected with a name from earlier list
		//put the texviews in an arraylists
		nameViewArray.add(namefirst);nameViewArray.add(namesecond);nameViewArray.add(namethird);nameViewArray.add(namefourth);nameViewArray.add(namefifth);nameViewArray.add(namesixth);
		nameViewArray.add(nameseventh);nameViewArray.add(nameeighth);nameViewArray.add(nameninth);nameViewArray.add(nametenth);
		scoreViewArray.add(scorefirst);scoreViewArray.add(scoresecond);scoreViewArray.add(scorethird);scoreViewArray.add(scorefourth);scoreViewArray.add(scorefifth);scoreViewArray.add(scoresixth);
		scoreViewArray.add(scoreseventh);scoreViewArray.add(scoreeighth);scoreViewArray.add(scoreninth);scoreViewArray.add(scoretenth);
		updateHighscoreLayout(this);
    }
	/**
	 *  Update the textviews in the layout.
	 *  
	 *  OBS you need to update the view by yourself
	 *  
	 * @param con - the context
	 */
	public void updateHighscoreLayout(Context con){
		//Get the list with earlier saved highscores
		ArrayList<Highscore> highscores = Preferences.getHighscore(con);
		if(highscores.size() == 0){ // if the list with earlier score is empty, then remove all the text in the texview arrays
			for(int i = 0;i < nameViewArray.size();i++){
				nameViewArray.get(i).setText("");
				scoreViewArray.get(i).setText("");
			}
		}else{
			//collect the highscores and put them in the textviews
			for(int i = 0;i < highscores.size();i++){
				nameViewArray.get(i).setText(highscores.get(i).getName().toString());
				scoreViewArray.get(i).setText(highscores.get(i).getScore().getScoreInt() + "");
			}
		}
	}
}
